import xrRequest from './xrRequest.js';
import xrApi from './xrApi.js';

module.exports = {
	// 提示
	msg:function(title, duration=1500, mask=false, icon='none'){
		//统一提示方便全局修改
		if(Boolean(title) === false){
			return;
		}
		uni.showToast({
			title,
			duration,
			mask,
			icon
		});
	},
	imgUrl(url){
		return xrApi.BASE_URL+url
	},
	isGetToken(){
		let token = uni.getStorageSync('token');
		if(token !== undefined && token !== null && token.length !== 0){
			return true;
		}else{
			return false;
		}
	},
	// 获取token，公交使用
	xrLogin() {
		// console.log('获取token');
		var that = this;
		xrRequest({
			url: xrApi.login,
			method: 'POST',
			data: {
				"loginType": "client",
				"clientname": "qsbskhd",
				"password": "Rfzj2hlX0h#y7TTP"
			},
			isShowLoading:false,
		}).then(res => {
			// console.log(JSON.stringify(res));
			if (res.code == 200) {
				// 更新token
				uni.setStorageSync('token', res.token);
				// 位置信息 华为不允许未经用户许可获取用户位置信息
				// that.location();
				// 获取openid
				that.getOpenid();
			} else {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
		}).catch(err => {
			console.log(JSON.stringify(err));
		});
	},
	// 获取/更新token
	replaceToken(){
		var that = this;
		xrRequest({
			url: xrApi.login,
			method: 'POST',
			data: {
				"loginType": "client",
				"clientname": "qsbskhd",
				"password": "Rfzj2hlX0h#y7TTP"
			},
			isShowLoading:false,
		}).then(res => {
			// console.log(JSON.stringify(res));
			if (res.code == 200) {
				// 更新token
				uni.setStorageSync('token', res.token);
			} else {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
		}).catch(err => {
			console.log(JSON.stringify(err));
		});
	},
	// 获取openid，公交收藏使用
	getOpenid(){
		// console.log('获取openid');
		let that = this;
		// #ifdef H5 || APP-PLUS || MP-ALIPAY
		uni.setStorageSync('openId','o1fWe6oE_obGxRBXUiYSFJ-bLUYI');//测试openId
		// #endif
		
		// #ifdef MP-WEIXIN
		uni.login({
			onlyAuthorize:true,
			success(res) {
				// console.log(JSON.stringify(res));
				if (res.code) {
					let header = {
						"content-type": "application/json",
						"Authorization": "Bearer " + uni.getStorageSync('token')
					};
					
					xrRequest({
						url: xrApi.userLogin,
						data: res.code,
						header: header,
						method: 'POST',
						timeout: 60000
						// isShowLoading:true,
						// lodingText:'加载中...'
					}).then(res => {
						// console.log('获取openid='+JSON.stringify(res));
						if (res.code == 200) {
							uni.setStorageSync('openId',res.data.openid);
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					}).catch(err => {
						console.log(JSON.stringify(err));
					});
				} else {
					console.log(res.errMsg);
				}
			},
		});
		// #endif
	},
	// 获取位置信息
	location(){
		let that = this;
		// #ifdef H5
		// let getData = {
		// 	// 国际会议中心东
		// 	"longitude": 106.646413,
		// 	"latitude": 26.635805
		// }
		// 国际会议中心
		let getData = {
			// 国际会议中心东
			"longitude": 106.649868,
			"latitude": 26.638242
		}
		// let getData = {
		// 	// 遵义
		// 	"longitude": 107.031922,
		// 	"latitude": 27.721931
		// }
		uni.setStorageSync('getData', getData);
		// #endif
		
		// 定位获取经纬度
		// #ifdef MP-WEIXIN || APP-PLUS || MP-ALIPAY
		// let getData = {
		// 	/**北京定位**/
		// 	"longitude": 118.796624,
		// 	"latitude": 32.059344
		// }
		// that.showlocerr = true;
		// uni.setStorageSync('getData', getData);
		uni.getLocation({
			type: 'gcj02',
			success: function(res) {
				// console.log(JSON.stringify(res));
				that.showlocerr = false;
				let getData = {
					"longitude": res.longitude,
					"latitude": res.latitude
				}
				uni.setStorageSync('getData', getData);
			},
			complete: function(res) {},
			fail: function(res) {
				console.log(JSON.stringify(res));
				that.showlocerr = true //定位失败
			}
		});
		// #endif
		this.getCity();
	},
	// 获取当前城市
	getCity(){
		let that = this;
		let header = {
			"content-type": "application/json",
			"Authorization": "Bearer " + uni.getStorageSync('token')
		};
		let locationData = uni.getStorageSync('getData');
		let query = {
			lng:locationData.longitude, //
			lat:locationData.latitude
		};
		xrRequest({
			url: xrApi.getCity,
			data: query,
			header: header
			// isShowLoading: true,
			// lodingText: '加载中...'
		}).then(res => {
			// console.log(JSON.stringify(res));
			if (res.code == 200) {
				uni.setStorageSync('cityData',res.data);
			} else{
				console.log('定位出错1',JSON.stringify(res));
				/**使用贵阳定位**/
				let getData = {
					"longitude": 106.649868,
					"latitude": 26.638242
				}
				uni.setStorageSync('getData', getData);
				let citrData = {
					adcode: "520115",
					city: "贵阳市",
					province: "贵州省",
				}
				uni.setStorageSync('cityData',citrData);
				console.log('ffff')
			}
		}).catch(err => {
			console.log('定位出错2',JSON.stringify(err));
			/**使用贵阳定位**/
			let getData = {
				"longitude": 106.649868,
				"latitude": 26.638242
			}
			uni.setStorageSync('getData', getData);
			let citrData = {
				adcode: "520115",
				city: "贵阳市",
				province: "贵州省",
			}
			uni.setStorageSync('cityData',citrData);
		});
	}
	
}